package com.lms.hadoop;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.yarn.webapp.hamlet2.Hamlet;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author: 李孟帅
 * @CreateTime: 2020-08-28 08:56
 * @Description:
 */
public class HadoopClientTest {

    FileSystem fs = null;
    Configuration conf=null;

    @Before
    public void init() throws IOException, URISyntaxException, InterruptedException {
         conf = new Configuration();
//        conf.set("fs.defaultFS","hdfs://lms-test-100:9000");
        fs = FileSystem.get(new URI("hdfs://lms-test-100:9000"),conf,"hadoop");

    }

    /**
    * @Author 李孟帅
    * @Description 查看默认配置
    * @Date 2020/8/28
    **/
    @Test
    public void defaultConfig(){
        Iterator<Map.Entry<String, String>> iterator = conf.iterator();
        iterator.forEachRemaining(entry-> System.out.println(entry.getKey()+":"+entry.getValue()));
    }

    /**
    * @Author 李孟帅
    * @Description 上传文件
    * @Date 2020/8/28
    **/
    @Test
    public void upload() throws IOException {
        fs.copyFromLocalFile(new Path("c:/windows-version.txt"), new Path("/"));
        fs.close();
    }

    /**
    * @Author 李孟帅
    * @Description 下载文件
    * @Date 2020/8/28
    **/
    @Test
    public void download() throws IOException {
        fs.copyToLocalFile(new Path("/windows-version.txt"),new Path("d:/"));
        fs.close();
    }
}
